﻿using System.Collections.Generic;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using QuickUIT.Controls;

namespace QuickUIT.Tests
{
    [TestClass]
    public class UIDataItemTests
    {
        private BaseState _baseState;

        [TestInitialize]
        public void TestSetup()
        {
            _baseState = new BaseState(TestUtilities.GetTestApplicationFileName(), "@Name='QuickUIT Test Application'");
            _baseState.Execute();
        }

        [TestMethod]
        public void ShouldGetDefaultValueForUnsupportedColumnProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.AreEqual(-1, dataItem.Column);
        }

        [TestMethod]
        public void ShouldGetDefaultValueForUnsupportedColumnSpanProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.AreEqual(-1, dataItem.ColumnSpan);
        }

        [TestMethod]
        public void ShouldGetDefaultValueForUnsupportedRowProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.AreEqual(-1, dataItem.Row);
        }

        [TestMethod]
        public void ShouldGetDefaultValueForUnsupportedRowSpanProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.AreEqual(-1, dataItem.RowSpan);
        }

        [TestMethod]
        public void ShouldGetDefaultValueForUnsupportedValueProperty()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsNull(dataItem.Value);
        }

        [TestMethod]
        public void ShouldSelect()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsFalse(dataItem.IsSelected);
            dataItem.Select();
            Assert.IsTrue(dataItem.IsSelected);
        }

        [TestMethod]
        public void ShouldAddToSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            dataItem.AddToSelection();
            Assert.IsTrue(dataItem.IsSelected);
        }

        [TestMethod]
        public void ShouldRemoveFromSelection()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsFalse(dataItem.IsSelected);
            dataItem.Select();
            Assert.IsTrue(dataItem.IsSelected);
            dataItem.RemoveFromSelection();
            Assert.IsFalse(dataItem.IsSelected);
        }

        [TestMethod]
        public void ShouldGetSelectionContainer()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            var selectionContainer = dataItem.GetSelectionContainer(); 
            Assert.IsNotNull(selectionContainer);
            Assert.AreEqual("dataGrid1", selectionContainer.Current.AutomationId);
        }

        [TestMethod]
        public void ShouldScrollIntoView()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            dataItem.ScrollIntoView();
            Assert.IsFalse(dataItem.IsOffscreen);
        }

        [TestMethod]
        public void ShouldGetExpandCollapseState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsFalse(dataItem.ExpandCollapseState.HasValue);
        }

        [TestMethod]
        public void ShouldGetIsExpanded()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsFalse(dataItem.IsExpanded);
        }

        [TestMethod]
        public void ShouldGetToggleState()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            UIDataItem dataItem = window.UIDataGrid("@AutomationId='dataGrid1'").UIDataItem();
            Assert.IsFalse(dataItem.ToggleState.HasValue);
        }

        [TestMethod]
        public void ShouldGetAllDataItems()
        {
            var window = new UIWindow("@Name='QuickUIT Test Application'");
            var dataGrid = window.UIDataGrid("@AutomationId='dataGrid1'"); 
            List<UIDataItem> dataItems = dataGrid.UIDataItems();
            Assert.AreEqual(6, dataItems.Count);
        }

        [TestCleanup]
        public void TestTeardown()
        {
            _baseState.Cleanup();
        }
    }
}